为了账号安全,请及时绑定邮箱和手机立即绑定

Spring 基于设值函数的依赖注入

标签:
Spring

Spring 基于设值函数的依赖注入

当容器调用一个无参的构造函数或一个无参的静态 factory 方法来初始化你的 bean 后,通过容器在你的 bean 上调用设值函数,基于设值函数的 DI 就完成了。

示例:

下述例子显示了一个类 TextEditor,它只能使用纯粹的基于设值函数的注入来实现依赖注入。

让我们用 Eclipse IDE 适当地工作,并按照以下步骤创建一个 Spring 应用程序。

步骤描述
1创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建包 com.tutorialspoint 。
2使用 Add External JARs 选项添加必需的 Spring 库,解释见 Spring Hello World Example chapter.
3在 com.tutorialspoint 包下创建 Java类 TextEditorSpellChecker 和 MainApp
4在 src 文件夹下创建 Beans 的配置文件 Beans.xml 。
5最后一步是创建所有 Java 文件和 Bean 配置文件的内容并按照如下所示的方法运行应用程序。

下面是 TextEditor.java 文件的内容:

package com.tutorialspoint; public class TextEditor {    private SpellChecker spellChecker;    // a setter method to inject the dependency.    public void setSpellChecker(SpellChecker spellChecker) {       System.out.println("Inside setSpellChecker." );       this.spellChecker = spellChecker;    }    // a getter method to return spellChecker    public SpellChecker getSpellChecker() {       return spellChecker;    }    public void spellCheck() {       spellChecker.checkSpelling();    } }

在这里,你需要检查设值函数方法的名称转换。要设置一个变量 spellChecker,我们使用 setSpellChecker() 方法,该方法与 Java POJO 类非常相似。让我们创建另一个依赖类文件 SpellChecker.java 的内容:

package com.tutorialspoint; public class SpellChecker {    public SpellChecker(){       System.out.println("Inside SpellChecker constructor." );    }    public void checkSpelling() {       System.out.println("Inside checkSpelling." );    }   }

以下是 MainApp.java 文件的内容:

package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {    public static void main(String[] args) {       ApplicationContext context =               new ClassPathXmlApplicationContext("Beans.xml");       TextEditor te = (TextEditor) context.getBean("textEditor");       te.spellCheck();    } }

下面是配置文件 Beans.xml 的内容,该文件有基于设值函数注入的配置:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <!-- Definition for textEditor bean -->    <bean id="textEditor" class="com.tutorialspoint.TextEditor">       <property name="spellChecker" ref="spellChecker"/>    </bean>    <!-- Definition for spellChecker bean -->    <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">    </bean> </beans>

你应该注意定义在基于构造函数注入和基于设值函数注入中的 Beans.xml 文件的区别。唯一的区别就是在基于构造函数注入中,我们使用的是〈bean〉标签中的〈constructor-arg〉元素,而在基于设值函数的注入中,我们使用的是〈bean〉标签中的〈property〉元素。

第二个你需要注意的点是,如果你要把一个引用传递给一个对象,那么你需要使用 标签的 ref 属性,而如果你要直接传递一个值,那么你应该使用 value 属性。


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消